package com.lv.db.dao;

import android.content.Context;
import android.database.Cursor;

import com.alibaba.fastjson.JSON;
import com.lv.db.base.BaseDaoImpl;
import com.lv.db.control.DBControl;
import com.lv.db.entity.LinkMan;
import com.lv.utils.L;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Xulin on 2017/5/18
 * email xulin67@163.com.
 */

public class LinkManDaoImpl extends BaseDaoImpl implements LinkManDao<LinkMan> {
    private final String TAG = this.getClass().getSimpleName();

    public LinkManDaoImpl(Context context) {
        this.context = context;
    }

    @Override
    public void add(LinkMan linkMan) {
        try {
            dbControl = new DBControl(context);
            StringBuffer sqlbuf = new StringBuffer();
            sqlbuf.append("INSERT INTO " + linkMan.tableName() + "("
                    + linkMan._fname() + ","
                    + linkMan._fphone() + ","
                    + linkMan._fstate() + ","
                    + linkMan._fcontactid()+","
                    + linkMan._fspellsort()+","
                    + linkMan._fspelledall()+","
                    + linkMan._favatar()+") ");
            sqlbuf.append(" VALUES ");
            sqlbuf.append("(?,?,?,?,?,?,?);");
            sql = sqlbuf.toString();
            dbControl.exeSQL(sql,new Object[]{linkMan.getFname(),linkMan.getFphone(),linkMan.getFstate(),linkMan.getFcontactid(),linkMan.getFspellsort(),linkMan.getFspelledall(),linkMan.getFavatar()});
        } catch (Exception e) {
            L.e(TAG, e.toString());
        } finally {
            dbControl.closeDB();
            sql = null;
        }
    }

    @Override
    public void add(List<LinkMan> list) {
        try {
            dbControl = new DBControl(context);
            dbControl.beginTransaction();  //开始事务
            for (LinkMan linkMan : list) {
                StringBuffer sqlbuf = new StringBuffer();
                sqlbuf.append("INSERT INTO " + linkMan.tableName() + "("
                        + linkMan._fname() + ","
                        + linkMan._fphone() + ","
                        + linkMan._fstate() + ","
                        + linkMan._fcontactid()+","
                        + linkMan._fspellsort()+","
                        + linkMan._fspelledall()+","
                        + linkMan._favatar()+") ");
                sqlbuf.append(" VALUES ");
                sqlbuf.append("(?,?,?,?,?,?,?);");
                sql = sqlbuf.toString();
                dbControl.exeSQL(sql,new Object[]{linkMan.getFname(),linkMan.getFphone(),linkMan.getFstate(),linkMan.getFcontactid(),linkMan.getFspellsort(),linkMan.getFspelledall(),linkMan.getFavatar()});
            }
        } catch (Exception ex) {
            L.e(TAG, ex.toString());
        } finally {
            dbControl.endTransaction();
            dbControl.closeDB();
            sql = null;
        }
    }

    @Override
    public void delete(int id) {

    }

    @Override
    public void deletePseudo(int id) {

    }

    @Override
    public void update(LinkMan linkMan) {

    }

    @Override
    public List<LinkMan> find(Map<String, Object> map) {
        LinkMan linkMan = new LinkMan();
        List<LinkMan> list = null;
        try {
            dbControl = new DBControl(context);
            sql = "select * from " + linkMan.tableName() + " where 1=1 order by "+linkMan._fspellsort()+"," +linkMan._fname()+" ASC ";
            if (map != null) {
            }
            cursor = dbControl.openDB(sql);
            if (cursor != null) {
                list = new ArrayList<LinkMan>();
                while (cursor.moveToNext()) {
                    list.add(getEntityByCursor(cursor));
                }
            }
        } catch (Exception e) {
            L.e(TAG, e.toString());
        } finally {
            cursor.close();
            dbControl.closeDB();
            sql = null;
        }
        return list;
    }

    @Override
    public LinkMan findById(int id) {
        LinkMan linkMan = new LinkMan();
        try {
            dbControl = new DBControl(context);
            sql = "select * from " + linkMan.tableName() + " where 1=1 and " + linkMan._fid() + " = '" + linkMan.getFid() + "'";
            cursor = dbControl.openDB(sql);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    linkMan = getEntityByCursor(cursor);
                }
            }
        } catch (Exception e) {
            L.e(TAG, e.toString());
        } finally {
            cursor.close();
            dbControl.closeDB();
            sql = null;
        }
        return linkMan;
    }

    @Override
    public void deleteAll() {
        try {
            dbControl = new DBControl(context);
            sql = "delete from " + new LinkMan().tableName() + " where 1=1 ";
            dbControl.exeSQL(sql, new Object[]{});
        } catch (Exception e) {
            L.e(TAG, e.toString());
        } finally {
            dbControl.closeDB();
            sql = null;
        }
    }

    @Override
    public Map<String, Object> getAll() {
        LinkMan linkMan = new LinkMan();
        Map<String, Object> baseMap = null;
        try {
            dbControl = new DBControl(context);
            sql = "select * from " + linkMan.tableName() + " where 1=1 ";
            cursor = dbControl.openDB(sql);
            if (cursor != null) {
                baseMap = new HashMap<String, Object>();
                while (cursor.moveToNext()) {
                    LinkMan man = new LinkMan();
                    //不可扩展，此处用于比对电话薄
                    man.setFname(cursor.getString(cursor.getColumnIndex(linkMan._fname())));
                    man.setFcontactid(cursor.getString(cursor.getColumnIndex(linkMan._fcontactid())));
                    man.setFphone(cursor.getString(cursor.getColumnIndex(linkMan._fphone())));
                    baseMap.put(man.getFcontactid(), JSON.toJSONString(man));
                }
            }
        } catch (Exception e) {
            L.e(TAG, e.toString());
        } finally {
            cursor.close();
            dbControl.closeDB();
            sql = null;
        }
        return baseMap;
    }


    private LinkMan getEntityByCursor(Cursor cursor) {
        LinkMan linkMan = new LinkMan();
        linkMan.setFid(cursor.getInt(cursor.getColumnIndex(linkMan._fid())));
        linkMan.setFcontactid(cursor.getString(cursor.getColumnIndex(linkMan._fcontactid())));
        linkMan.setFname(cursor.getString(cursor.getColumnIndex(linkMan._fname())));
        linkMan.setFphone(cursor.getString(cursor.getColumnIndex(linkMan._fphone())));
        linkMan.setFavatar(cursor.getString(cursor.getColumnIndex(linkMan._favatar())));
        linkMan.setFstate(cursor.getString(cursor.getColumnIndex(linkMan._fstate())));
        linkMan.setFspelledall(cursor.getString(cursor.getColumnIndex(linkMan._fspelledall())));
        linkMan.setFspellsort(cursor.getString(cursor.getColumnIndex(linkMan._fspellsort())));
        return linkMan;
    }

    @Override
    public void updateByContactId(LinkMan linkMan) {
        try {
            dbControl = new DBControl(context);
            sql = "update " + new LinkMan().tableName() + " set "
                    + linkMan._fphone() + "= ? ,"
                    + linkMan._fname() + "= ? ,"
                    + linkMan._favatar() + "= ? ,"
                    + linkMan._fspelledall() + "= ? ,"
                    + linkMan._fspellsort() + "= ? ,"
                    + linkMan._fstate() + "= ? "
                    +"  where 1=1 and "+ linkMan._fcontactid() +"= ?";
            dbControl.exeSQL(sql, new Object[]{linkMan.getFphone(), linkMan.getFname(), linkMan.getFavatar(),linkMan.getFspelledall(),linkMan.getFspellsort(), linkMan.getFstate(),linkMan.getFcontactid()});
        } catch (Exception e) {
            L.e(TAG, e.toString());
        } finally {
            dbControl.closeDB();
            sql = null;
        }
    }

    @Override
    public void deleteByContactId(LinkMan linkMan) {
        try {
            dbControl = new DBControl(context);
            sql = "delete from  " + new LinkMan().tableName()
                    +"  where 1=1 and "+ linkMan._fcontactid() +"= ?";
            dbControl.exeSQL(sql, new Object[]{linkMan.getFcontactid()});
        } catch (Exception e) {
            L.e(TAG, e.toString());
        } finally {
            dbControl.closeDB();
            sql = null;
        }
    }
}
